package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Scoring;
import com.ljc.cjgl.entitiy.TbTest;
import com.ljc.cjgl.form.ScoringForm;
import com.ljc.cjgl.service.ScoringService;

@Controller
@RequestMapping(value="/scoring")
public class ScoringController {
	@Autowired
	private ScoringService service;
	
	@RequestMapping(value="manage")
	public String manage(ModelMap map) {
		List<TbTest> tests = service.findAllTest();
		map.put("tests",tests);
		return "scoring/manage";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageable,ScoringForm form) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		HashSet<Rule> rules=new HashSet<>();
		String title = form.getTitle();
		Integer testId = form.getTestId();
		String search = form.getSearch();
		if(StringUtils.hasText(title)) {
			rules.add(Rule.property("title").contain(title));
		}
		if(testId!=null) {
			rules.add(Rule.property("test.id").equal(testId));
		}
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("test.name").contain(search));
			rules2.add(Rule.property("title").contain(search));
			rules2.add(Rule.property("explain").contain(search));
			rules.add(Rule.or(rules2));
		}
		Specification<Scoring> specification = Rule.and(rules).toSpecification();
		Page<Scoring> page=service.findAll(specification,pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	public String edit(Integer id,Integer testId,ModelMap map) {
		Scoring model=new Scoring();
		if(id!=null) {
			model=service.findById(id);
		}
		map.put("model", model);
		List<TbTest> tests=service.findAllTest();
		if(testId!=null) {
			tests.clear();
			tests.add(service.findTestById(testId));
		}
		map.put("tests", tests);
		return "scoring/edit";
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(ScoringForm form) {
		service.save(form);
		return TableResult.bulidResult(true, "数据保存成功");
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestBody List<Scoring> scorings) {
		if(service.delete(scorings)) {
			return TableResult.bulidResult(true, "数据删除成功");
		}else {
			return TableResult.bulidResult(false, "数据删除失败");
		}
	}
}
